//
//  AddressManagementCell.m
//  Yanwei
//
//  Created by 刘文 on 17/5/4.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "AddressManagementCell.h"
#import "NSString+Util.h"

@interface AddressManagementCell ()

@property (strong, nonatomic) UILabel * nameLabel;
@property (strong, nonatomic) UILabel * phoneLabel;
@property (strong, nonatomic) UILabel * addressLabel;
@property (nonatomic, strong) UIView * line;

@end

@implementation AddressManagementCell

+ (CGFloat)cellHeight:(StoreAddressModel *)model {
    CGFloat height = 44;
    
    NSString * str = model.name;
    CGSize size = TextSize(str, APPFONT(15));
    height += size.height;
    
    str = [[model.areaMergedName stringByAppendingString:@" "] stringByAppendingString:model.address];
    if (model.defaultSetting) {
        str = [NSString stringWithFormat:@"[默认地址] %@", str];
    }
    
    size = TextSize_MutiLine(str, APPFONT(13), CGSizeMake(ScreenWidth - 18 - 70, 50));
    height += size.height;
    
    return height;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.line = [[UIView alloc] init];
        self.line.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.line];
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
            make.top.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView);
            make.width.mas_equalTo(3);
        }];
        
        UIView * bottomLine = [UIView new];
        bottomLine.backgroundColor = Color217;
        [self.contentView addSubview:bottomLine];
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).with.offset(18);
            make.bottom.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
            make.height.mas_equalTo(0.5);
        }];
        
        self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.editButton.titleLabel.font = ICONFONT(16);
        [self.editButton setTitle:@"\uE691" forState:UIControlStateNormal];
        [self.editButton setTitleColor:Color140 forState:UIControlStateNormal];
        [self.contentView addSubview:self.editButton];
        [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView);
            make.top.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView);
            make.width.mas_equalTo(50);
        }];
    }
    return self;
}

- (void)setModel:(StoreAddressModel *)model {
    _model = model;
    self.nameLabel.text = _model.name;
    self.phoneLabel.text = _model.mobile;
    self.addressLabel.text = [[_model.areaMergedName stringByAppendingString:@" "] stringByAppendingString:_model.address];
    _line.backgroundColor = [UIColor whiteColor];
    if (model.defaultSetting) {
        _line.backgroundColor = Color175030;
        self.addressLabel.text = [@"[默认地址] " stringByAppendingString:self.addressLabel.text];
        // 同label不同字体大小
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.addressLabel.text];
        NSRange range = [self.addressLabel.text rangeOfString:@"]"];
        NSDictionary *rangBeforedic = @{NSForegroundColorAttributeName:Color175030, NSFontAttributeName:APPFONT(13)};
        NSDictionary *rangAfterdic = @{NSForegroundColorAttributeName:Color140, NSFontAttributeName:APPFONT(13)};
        [attrString addAttributes:rangBeforedic range:NSMakeRange(0, range.location+1)];
        [attrString addAttributes:rangAfterdic range:NSMakeRange(range.location+1, self.addressLabel.text.length - range.location-1)];
        [self.addressLabel setAttributedText:attrString];
    }
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = APPFONT(15);
        _nameLabel.textColor = Color34;
        [self.contentView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).with.offset(18);
            make.top.equalTo(self.contentView).with.offset(17);
        }];
    }
    return _nameLabel;
}

- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [UILabel new];
        _phoneLabel.font = APPFONT(15);
        _phoneLabel.textColor = Color34;
        [self.contentView addSubview:_phoneLabel];
        [_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel.mas_right).with.offset(10);
            make.top.equalTo(self.nameLabel);
        }];
    }
    return _phoneLabel;
}

- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = [UILabel new];
        _addressLabel.font = APPFONT(13);
        _addressLabel.textColor = Color140;
        _addressLabel.numberOfLines = 2;
        [self.contentView addSubview:_addressLabel];
        [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel);
            make.top.equalTo(self.nameLabel.mas_bottom).with.offset(10);
            make.right.equalTo(self.contentView).with.offset(-70);
        }];
    }
    return _addressLabel;
}

@end
